package sql

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
)

type List struct {
	Id        int
	Name      string
	OrgPath   string
	NewPath   string
	Base64Str string
}

// 查询数据
func QueryFileMoveList(query string) (result []List) {

	// 结果集合
	result = make([]List, 0)

	rows, err := Select(query)
	if err != nil {
		fmt.Printf("Failed to query data: %v\n", err)
		return
	}
	for rows.Next() {
		var list List

		err = rows.Scan(&list.Id, &list.Name, &list.OrgPath, &list.NewPath)
		if err != nil {
			fmt.Printf("Failed to read data: %v\n", err)
			continue
		}

		/* // 检查字段是否为NULL并进行处理
		if url.Valid {
			list.Url = url.String
		} */

		/**
		 * 你想要对一个结构体进行Base64加密,你首先需要将该结构体序列化为字节流,然后再对这个字节流进行Base64编码。
		 */
		listJsonData, _ := json.Marshal(list)
		// 将字节数组转换为Base64编码的字符串
		list.Base64Str = base64.StdEncoding.EncodeToString(listJsonData)
		result = append(result, list)

	}
	rows.Close()
	return result
}

type ServerList struct {
	Id        int
	Name      string
	Value     string
	Base64Str string
}

// 查询数据
func QueryServerStartList(query string) (result []ServerList) {

	// 结果集合
	result = make([]ServerList, 0)

	rows, err := Select(query)
	if err != nil {
		fmt.Printf("Failed to query data: %v\n", err)
		return
	}
	for rows.Next() {
		var list ServerList

		err = rows.Scan(&list.Id, &list.Name, &list.Value)
		if err != nil {
			fmt.Printf("Failed to read data: %v\n", err)
			continue
		}

		/* // 检查字段是否为NULL并进行处理
		if url.Valid {
			list.Url = url.String
		} */

		/**
		 * 你想要对一个结构体进行Base64加密,你首先需要将该结构体序列化为字节流,然后再对这个字节流进行Base64编码。
		 */
		listJsonData, _ := json.Marshal(list)
		// 将字节数组转换为Base64编码的字符串
		list.Base64Str = base64.StdEncoding.EncodeToString(listJsonData)
		result = append(result, list)

	}
	rows.Close()
	return result
}
